package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import project.pianpian.entity.*;
import project.pianpian.mapper.*;
import project.pianpian.utils.ReturnUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:
 * author :万霁初
 * date :2023/1/2 23:23
 **/
@RestController
@Slf4j
@RequestMapping("/main")
public class VideoWatching {
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    TagMapper tagMapper;
    @Autowired
    UpvoteMapper upvoteMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FollowMapper followMapper;

    //获取视频详情页面
    @GetMapping("/watch")
    public JSONObject watchVideo(@RequestParam(value = "videoId") int videoId){
        //获取视频信息
        Video video = videoMapper.selectById(videoId);
        //获取视频标签
        Map map=new HashMap();
        map.put("video_id",video.getId());
        List<Tag> tags = tagMapper.selectByMap(map);
        //获取视频点赞数量
        QueryWrapper<Upvote> upvoteQueryWrapper=new QueryWrapper<>();
        upvoteQueryWrapper.eq("video_id",video.getId());
        int upvoteCount = upvoteMapper.selectCount(upvoteQueryWrapper);
        //获取视频收藏数量
        QueryWrapper<Collect> collectQueryWrapper=new QueryWrapper<>();
        collectQueryWrapper.eq("video_id",video.getId());
        int collectCount=collectMapper.selectCount(collectQueryWrapper);
        //获取上传者粉丝数
        QueryWrapper<Follow> followQueryWrapper=new QueryWrapper<>();
        followQueryWrapper.eq("followed_id",video.getUploaderId());
        int followCount=followMapper.selectCount(followQueryWrapper);
        //将以上查询结果封装入JSON方便返回
        JSONObject returnJSON=new JSONObject();
        returnJSON.put("video",video);
        returnJSON.put("tags",tags);
        returnJSON.put("upvoteCount",upvoteCount);
        returnJSON.put("collectCount",collectCount);
        returnJSON.put("followCount",followCount);

        return ReturnUtils.success(200,"获取视频页面详情成功",returnJSON);
    }
}
